package com.spzx.user.controller;

import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.spzx.common.core.context.SecurityContextHolder;
import com.spzx.common.core.domain.R;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.annotation.RequiresLogin;
import com.spzx.user.domain.Region;
import com.spzx.user.domain.UserAddress;
import com.spzx.user.service.UserRegionService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.user.service.IUserAddressService;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.utils.poi.ExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 用户地址Controller
 *
 * @author atguigu
 * @date 2024-07-10
 */
@Tag(name = "用户地址接口管理")
@RestController
@RequestMapping("/userAddress")
public class UserAddressController extends BaseController
{
    @Resource
    private IUserAddressService userAddressService;

    @Resource
    private UserRegionService userRegionService;


    @GetMapping("/list")
    public AjaxResult getList()
    {
        List<UserAddress> userAddresslist = userAddressService.getAddressList();
        return AjaxResult.success(userAddresslist);
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Integer id){
        userAddressService.deleteById(id);
        return AjaxResult.success();
    }

    @PutMapping
    public AjaxResult update(@RequestBody UserAddress userAddress){

        String fullAddress = getFullAddress(userAddress);
        Long isDefault = userAddress.getIsDefault();
        if(isDefault == 1){
            Long userId = SecurityContextHolder.getUserId();
            UpdateWrapper<UserAddress> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("user_id",userId);
            UserAddress address = new UserAddress();
            address.setIsDefault(0L);
            userAddressService.update(address,updateWrapper);
        }
        userAddress.setFullAddress(fullAddress);
        userAddressService.updateById(userAddress);
        return AjaxResult.success();
    }

    @PostMapping
    public AjaxResult add(@RequestBody UserAddress userAddress){
        Long userId = SecurityContextHolder.getUserId();
        String fullAddress = getFullAddress(userAddress);
        userAddress.setUserId(userId);
        userAddress.setFullAddress(fullAddress);
        userAddressService.save(userAddress);
        return AjaxResult.success();
    }

    @InnerAuth
    @GetMapping(value = "/getUserAddress/{id}")
    public R<UserAddress> getUserAddress(@PathVariable("id") Long id){
        UserAddress userAddress = userAddressService.getById(id);
        return R.ok(userAddress);
    }


    public String getFullAddress(UserAddress userAddress){
        String provinceCode = userAddress.getProvinceCode();
        String cityCode = userAddress.getCityCode();
        String districtCode = userAddress.getDistrictCode();
        QueryWrapper<Region> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("code", Arrays.asList(provinceCode,cityCode,districtCode));
        List<Region> list = userRegionService.list(queryWrapper);
        return list.get(0).getName() + list.get(1).getName() + list.get(2).getName() + userAddress.getAddress();
    }

}
